package org.gnomus.server.controllers;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import org.gnomus.data.store;
import org.gnomus.server.Context;
import org.gnomus.server.Controller;

import java.util.*;

public class Score implements Controller {
    public String score;
    public String lesson_id;

    public boolean record() {
/*
        Context ctx = Context.get();
        ctx.ajax = true;
        if (ctx.account == null || score == null) {
            return false;
        }
        Key uk = store.s2k((String) ctx.account.get("k"));
        Key sk = store.put("score", score);
        store.link.put("score", uk, sk);
*/
        return true;
    }

    public boolean list() {
/*
        Context ctx = Context.get();
        List<Map> scores = new ArrayList<Map>();
        Set<String> lesson_ids = new HashSet<String>();
        if (ctx.account == null) {
            ctx.redirect(Context.us.createLoginURL(ctx.uri));
            return false;
        }
        Key uk = store.s2k((String) ctx.account.get("k"));
        Iterable<Entity> score_links = store.link.get("score", uk, null);
        for (Entity link : score_links) {
            Entity score = store.get((Key) link.getProperty("to"));
            scores.add(score.getProperties());
            lesson_ids.add((String) score.getProperty("lesson_id"));
        }

        List<Map> lessons = new ArrayList<Map>();
        for (String id : lesson_ids) {
            lessons.add(store.get("lesson", id).getProperties());
        }
        ctx.data.put("lessons", lessons);
*/
        return true;
    }

    public boolean view() {
/*
        Context ctx = Context.get();
        if (ctx.account == null) {
            ctx.redirect(Context.us.createLoginURL(ctx.uri));
        }
        if (lesson_id == null) {
            return true;
        }
        Entity lesson = store.get("lesson", lesson_id);
        if (lesson == null) {
            return true;
        }
        ctx.data.put("lesson", store.getData(lesson));

        List<Map> scores = new ArrayList<Map>();
        Key uk = store.s2k((String) ctx.account.get("k"));
        Iterable<Entity> score_links = store.link.get("score", uk, null);
        for (Entity link : score_links) {
            Entity score = store.get((Key) link.getProperty("to"));
            if (lesson_id.equals(score.getProperty("lesson_id"))) {
                Entity item = store.get("item", (String) score.getProperty("item_id"));
                if (item == null) {
                    continue;
                }
                Map<String, Object> score_details = new HashMap<String, Object>(score.getProperties());
                score_details.put("item", item.getProperties());
                scores.add(score_details);
            }
        }
        ctx.data.put("scores", scores);
*/
        return true;
    }
}
